package com.service.scheduling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.mapper.CoinsPriceMapper;
import com.service.dao.model.CoinsPrice;
import com.service.scheduling.service.CoinsPriceService;
import com.service.scheduling.service.TradingSpotTradesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年09月27日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Service
public class CoinsPriceServiceImpl extends ServiceImpl<CoinsPriceMapper, CoinsPrice> implements CoinsPriceService {
    @Autowired
    private TradingSpotTradesService spotTradesService;

    @Override
    public CoinsPrice getLastCoinsPrice(Integer coinsId) {
        return getOne(new QueryWrapper<CoinsPrice>().lambda()
                .eq(CoinsPrice::getCoinsId, coinsId)
                .orderByDesc(CoinsPrice::getCreateTime).last("limit 1"));
    }

    @Override
    public List<CoinsPrice> getCoinsPrice() {
        return baseMapper.selectNewestPrice(CommonStatusEnums.ACTIVE.getCode());
    }

    @Override
    public Map<String, BigDecimal> getStandardPrice2Map(boolean needActualPrice) {
        Map<String, BigDecimal> resultMap = this.getCoinsPrice().stream()
                .collect(Collectors.toMap(CoinsPrice::getCoinsName, CoinsPrice::getStandardPrice));
        if(needActualPrice){
            Map<String, BigDecimal> actualPrice = spotTradesService.getActualTimePrice();
            if(!ObjectUtils.isEmpty(actualPrice)){
                resultMap.putAll(actualPrice);
            }
        }
        return resultMap;
    }

}
